from __future__ import division
from operator import itemgetter, attrgetter
from struct import *
import gc
import math
import os
import random
import sys
import time
from sets import Set
from random import randint
import re
import numpy as np
from os import walk


print "Begins..."
ifn = sys.argv[1]
print "ifn:",ifn
ifh = open(ifn,"r")

currentLine = ifh.readline()
nextLine = ""
currentQID = ""

processFlag = True
outputLine = ""

while currentLine:
    if currentLine.startswith("qid:"):
        currentQID = currentLine.strip().split(":")[1].strip()
        print "qid:",currentQID
    # sample parsing line:
    # Search: u s  oil industry history
    if currentLine.startswith("Search:") and processFlag:
        elementList = currentLine.strip().split(" ")
        
        currentSearchContent = ""
        
        queryTermList = []
        for element in elementList[1:]:
            if element.strip() != "":
                queryTermList.append( element.strip() )
        
        for element in queryTermList:
            currentSearchContent += element + " "
        
        print "QueryContent:",currentSearchContent
        
        
        currentQueryTermIndexDict = {}
        nextLine = ifh.readline()
        print nextLine.strip()
        
        parsedFlag = True
        if nextLine.strip().endswith("is NOT in the lexicon."):
            parsedFlag = False
        
        if nextLine.strip() != "" and parsedFlag:
            nextLineElements = nextLine.strip().split(" ")
            # print "nextLineElements:",nextLineElements
            
            # sample parsing line: 
            # oil:0 industry:1 history:2 u:3 s:4
            for element in nextLineElements:
                if element.split(":")[0] not in currentQueryTermIndexDict:
                    currentQueryTermIndexDict[ int(element.split(":")[1]) ] = element.split(":")[0]
            
            print "currentQueryTermIndexDict:",currentQueryTermIndexDict
            print "len(currentQueryTermIndexDict):",len(currentQueryTermIndexDict)
            
            upperBound = 0
            if len(currentQueryTermIndexDict) > 10:
                upperBound = 10 
            else:
                upperBound = len(currentQueryTermIndexDict)
            # ignore index_use_precomputed_score_:0
            # ignore universal_threshold_socre_of_posting_: 0
            ifh.readline()
            ifh.readline()
            
            # Now, it is time to read the actual training example line
            currentLine = ifh.readline()
            # print currentLine.strip()
            currentLineElements = currentLine.strip().split(" ")
            while len(currentLineElements) == 24 and currentLineElements[-2].startswith("GX"):
                base = 1
                for index in range(0,upperBound):
                    if float(currentLineElements[base+index]) != 0.0: 
                        print currentLineElements[0],currentQueryTermIndexDict[index],currentLineElements[-2]
                currentLine = ifh.readline()
                # print currentLine.strip()
                currentLineElements = currentLine.strip().split(" ")
    currentLine = ifh.readline()

ifh.close()

print "Ends."




















